<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\service;

class Module extends \suifeng\mvc\Service
{
    /**
     * 获取模块列表，支持缓存。
     *
     * @return array
     */
    public function get_all()
    {
        $list = cache('admin_module_service::get_all');
        if (!$list || APP_DEBUG) {
            $where['status'] = array('eq', 1);
            $order = 'sort asc, id asc';

            $rows = model('admin/module')->where($where)
                ->order($order)
                ->select();

            $list = array();
            foreach ($rows as $row) {
                $row['config'] = $row['config'] ? json_decode($row['config']) : [];
                $row['admin_menu'] = $row['admin_menu'] ? json_decode($row['admin_menu']) : [];
                $row['user_nav'] = $row['user_nav'] ? json_decode($row['user_nav']) : [];
                $list[$row['name']] = $row->getData();
            }

            cache('admin_module_service::get_all', $list);
        }
        return $list;
    }

    /**
     * 清除缓存。
     */
    public function clear_cache()
    {
        cache('admin_module_service::get_all', null);
    }

}
